#!/bin/bash

# Read POST data and extract the target
read -n $CONTENT_LENGTH POST_DATA

# Function to parse POST data
parse_post() {
  echo "$POST_DATA" | grep -oP "(?<=$1=)[^&]+"
}

target=$(parse_post "target")
action=$(parse_post "action")

# Header for HTML response
echo "Content-Type: text/html"
echo ""

# HTML Output
echo "<html><body>"

if [[ "$action" == "ping" ]]; then
  echo "<h1>Ping Results for $target</h1><pre>"
  ping -c 10 "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "iperf" ]]; then
  echo "<h1>iPerf Results for $target</h1><pre>"
  iperf3 -c "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "traceroute" ]]; then
  echo "<h1>Traceroute Results for $target</h1><pre>"
  traceroute "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "whois" ]]; then
  echo "<h1>WhoIs Results for $target</h1><pre>"
  whois "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "dig" ]]; then
  echo "<h1>Dig Results for $target</h1><pre>"
  dig "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "http" ]]; then
  echo "<h1>HTTP Response Headers for $target</h1><pre>"
  curl -I "$target" 2>&1
  echo "</pre>"

elif [[ "$action" == "10mibfile" ]]; then
  echo "<h1>10MiB File Download Test</h1><pre>"
  wget -O /tmp/10mib_testfile https://speed.hetzner.de/10MB.bin
  if [[ $? -eq 0 ]]; then
    echo "Success......"
  else
    echo "Failed......"
  fi
  rm -f /tmp/10mib_testfile
  echo "</pre>"

elif [[ "$action" == "100mibfile" ]]; then
  echo "<h1>100MiB File Download Test</h1><pre>"
  wget -O /tmp/100mib_testfile https://speed.hetzner.de/100MB.bin
  if [[ $? -eq 0 ]]; then
    echo "Success......"
  else
    echo "Failed......"
  fi
  rm -f /tmp/100mib_testfile
  echo "</pre>"

else
  echo "<h1>Hello, World</h1>"
fi

# Closing HTML tags
echo "</body></html>"

